<title>模块管理</title>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-module mybtn" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-module mybtn" data-type="add">添加</button>
                <button class="layui-btn layuiadmin-btn-module mybtn" lay-filter="LAY-module-front-search" data-type="search"
                        style="float: right;margin: 0">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
                <input style="width: 200px; float: right" type="text" onclick=""
                       name="moduleinfo"
                       value="" placeholder="请输入要查询的模块名称" autocomplete="off" class="layui-form layui-input">

            </div>

            <table id="LAY-user-module" lay-filter="LAY-user-module"></table>

            <script type="text/html" id="table-module">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>


<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table
            , form = layui.form
            , admin = layui.admin
            , view = layui.view
            , $ = layui.$
        var sys_lis = [], menu_obj = {}
        // 获取所有子系统的接口
        $.ajax({
            // url: "./json/menu.js?v={{ layui.admin.v }}", // 测试接口
            url: layui.setter.hostAddr +"/portal/gnmk/toplevel",
            type: "GET",
            async: false,
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
            },
            success: function (res) {
                layui.data('menu', {
                    key: "sys_lis",
                    value: res.data
                })
                for (let i = 0; i < res.data.length; i++) {
                    sys_lis.push(res.data[i].gnmkId)
                }
            }
        })

        // 获取二级菜单的接口
        for (let i = 0; i < sys_lis.length; i++) {
            $.ajax({
                // url: "./json/menu.js?v={{ layui.admin.v }}", // 测试接口
                url: layui.setter.hostAddr +`/portal/gnmk/gnmkbypid?pid=${sys_lis[i]}`,
                type: "GET",
                dataType: "json",
                async: false,
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                },
                success: function (res) {
                    menu_obj[sys_lis[i]] = res.data
                    layui.data('menu', {
                        key: "menu_obj",
                        value: menu_obj
                    })
                }
            })

        }

        console.log(menu_obj)
        table.set({
            headers: { //通过 request 头传递
                Authorization: layui.data('layuiAdmin').Authorization
            }
        });
        table.render({
            elem: "#LAY-user-module"
            // , url: './json/module/testallmodule.js' 测试接口
            // , url: 'http://10.0.23.97:8195/portal/gnmk/bignmklist'
            , url: layui.setter.hostAddr +'/portal/gnmk/bignmklist'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'gnmkId', width: 100, title: 'ID', sort: true}
                , {field: 'gnmkName', title: '模块名称', minWidth: 100}
                , {
                    field: 'secondLevelGnmk', title: '所属菜单', minWidth: 100, templet: function (res) {
                        if (res.secondLevelGnmk) {
                            return res.secondLevelGnmk.gnmkName;
                        } else {
                            return ''
                        }
                    }
                }
                , {
                    field: 'topLevelGnmk', title: '所属子系统', templet: function (res) {
                        if (res.topLevelGnmk) {
                            return res.topLevelGnmk.gnmkName;
                        } else {
                            return ''
                        }
                    }
                }
                , {field: 'iframe', title: '服务地址'}
                , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-module'}
            ]]
            , page: true
            , height: 'full-320'
            , text: '加载出现异常！'
        })

        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-module')
                    , checkData = checkStatus.data, delobjs = []; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                for (let i = 0; i < checkData.length; i++) {
                    delobjs.push(checkData[i].gnmkId)
                }
                console.log("要删除的数据====" + JSON.stringify(delobjs))
                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    layer.close(index);

                    layer.confirm('确定删除吗？', function (index) {
                         $.ajax({
                            url: layui.setter.hostAddr +`/portal/gnmk/gnmk`,
                            dataType: 'json',
                            data: JSON.stringify(delobjs),
                            async: false,
                            beforeSend: function (XMLHttpRequest) {
                                XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                                XMLHttpRequest.setRequestHeader("Content-Type", "application/json");

                            },
                            type: 'DELETE',
                            success: function (res) {
                                // console.log("删除结果====" + JSON.stringify(res))
                                doSuccess()
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                //通常情况下textStatus和errorThrown只有其中一个包含信息
                                console.log(textStatus);   //调用本次ajax请求时传递的options参数
                                console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                                doFail()
                            }

                        })
                        doSuccess()
                        table.reload('LAY-user-module');

                    });
                });
            }
            , add: function () {
                admin.popup({
                    title: '添加模块'
                    , area: ['500px', '450px']
                    , success: function (layero, index) {
                        view(this.id).render('module/addmoduleSelect').done(function () {
                            form.render(null, 'layuiadmin-form-module');

                            //监听提交
                            form.on('submit(LAY-module-front-submit)', function (data) {
                                var addModulefields = data.field; //获取提交的字段
                                console.log("添加模块的数据===" + JSON.stringify(addModulefields))
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                $.ajax({
                                    url: layui.setter.hostAddr +"/portal/gnmk/bignmk",
                                    dataType: "json",
                                    contentType: "application/json",
                                    data: JSON.stringify(addModulefields),
                                    async: false,
                                    beforeSend: function (XMLHttpRequest) {
                                        XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                                    },
                                    type: 'POST',
                                    success: function (res) {
                                        doSuccess()
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        //通常情况下textStatus和errorThrown只有其中一个包含信息
                                        console.log(textStatus);   //调用本次ajax请求时传递的options参数
                                        console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                                        doFail()
                                    }
                                })
                                layui.table.reload('LAY-user-module'); //重载表格
                                // location.reload()
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            }
            , search: function () {
                var moduleVal = $("input[name='moduleinfo']").val()

                //执行重载
                table.reload('LAY-user-module', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        keyword:moduleVal
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-module').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('tool(LAY-user-module)', function (obj) {
            var allModuleData = obj.data, delobj = [];
            console.log(allModuleData)
            delobj.push(allModuleData.gnmkId)
            if (obj.event === 'del') {
                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    var del_data = {id: index, passcode: value}
                    console.log(delobj)
                    layer.close(index);

                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: layui.setter.hostAddr +`/portal/gnmk/gnmk`,
                            dataType: 'json',
                            data: JSON.stringify(delobj),
                            async: false,
                            beforeSend: function (XMLHttpRequest) {
                                XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                                XMLHttpRequest.setRequestHeader("Content-Type", "application/json");

                            },
                            type: 'DELETE',
                            success: function (res) {
                                // console.log("删除结果====" + JSON.stringify(res))
                                doSuccess()
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                //通常情况下textStatus和errorThrown只有其中一个包含信息
                                console.log(textStatus);   //调用本次ajax请求时传递的options参数
                                console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                                doFail()
                            }

                        })
                        obj.del();
                        table.reload('LAY-user-module');
                        layer.close(index);
                    });
                });
            } else if (obj.event === 'edit') {
                admin.popup({
                    title: '编辑模块'
                    , area: ['500px', '450px']
                    , id: 'LAY-popup-module-add'
                    , success: function (layero, index) {
                        view(this.id).render('module/addmoduleSelect', allModuleData).done(function () {
                            // view(this.id).render('user/user/userform', data).done(function(){
                            form.render(null, 'layuiadmin-form-module');

                            //监听提交
                            form.on('submit(LAY-module-front-submit)', function (data) {
                                var editModulefields = data.field; //获取提交的字段
                                editModulefields['gnmkId'] = allModuleData.gnmkId
                                console.log("编辑后的信息===" + JSON.stringify(editModulefields))
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                $.ajax({
                                    url: layui.setter.hostAddr +"/portal/gnmk/bignmk",
                                    dataType: "json",
                                    contentType: "application/json",
                                    data: JSON.stringify(editModulefields),
                                    async: false,
                                    beforeSend: function (XMLHttpRequest) {
                                        XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                                    },
                                    type: 'PUT',
                                    success: function (res) {
                                        doSuccess()
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        //通常情况下textStatus和errorThrown只有其中一个包含信息
                                        console.log(textStatus);   //调用本次ajax请求时传递的options参数
                                        console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                                        doFail()
                                    }
                                })
                                layui.table.reload('LAY-user-module'); //重载表格
                                layer.close(index); //执行关闭
                                location.reload()
                            });
                        });
                    }
                });
            }
        });


    })
</script>